package com.yocard.yxyd.ui.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.request.CertRequest;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.ui.contract.CertContract;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.RxUtil;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created on 2019/4/17.
 */
public class CertPresenter extends RxPresenter<CertContract.View> implements CertContract.Presenter {
    // 资格认证照片上传attachType
    private static final String QUALIFICATION_CERTIFICATION_TYPE = "QualificationCertification";

    @Inject
    public CertPresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void switchCity(OnPickListener listener, FragmentActivity activity) {
        CityPicker.from(activity)
                .enableAnimation(true)
                .setAnimationStyle(R.style.DefaultCityPickerAnimation)
                .setLocatedCity(null)
                .setOnPickListener(listener)
                .show();
    }

    @Override
    public void initLocation(Context context) {
        AndPermission.with(context)
                .runtime()
                .permission(Permission.ACCESS_FINE_LOCATION)
                .onGranted(permissions -> {
                    AMapLocationClient locationClient = new AMapLocationClient(AppContext.getContext());
                    AMapLocationClientOption mOption = new AMapLocationClientOption();
                    mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                    mOption.setGpsFirst(false);
                    mOption.setNeedAddress(true);
                    mOption.setHttpTimeOut(10000);
                    mOption.setInterval(2000);
                    mOption.setOnceLocation(true);
                    locationClient.setLocationOption(mOption);
                    mView.onLocationInit(locationClient);
                })
                .onDenied(permissions -> {
                    ToastUtils.showLong("请授予APP定位权限");
                })
                .start();
    }

    @SuppressLint("CheckResult")
    @Override
    public void uploadImage(String type, String fileUrl, ImageView iv, Uri uri) {
        addSubscribe(Observable.create(new ObservableOnSubscribe<File>() {
            @Override
            public void subscribe(ObservableEmitter<File> emitter) throws Exception {
                //压缩反而占用大量内存，oom
                File file = CommonUtils.saveImg(Constants.DEFAULT_PIC_SIZE, fileUrl, type);
//                File file = new File(fileUrl);
                emitter.onNext(file);
            }
        })
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(file -> {
                    addSubscribe(httpManager.uploadImg(type, file, QUALIFICATION_CERTIFICATION_TYPE)
                            .compose(RxUtil.rxSchedulerHelper())
                            .subscribeWith(new CommonSubscriber<CommonBean>(mView) {
                                @Override
                                public void onSuccess(CommonBean data) {
                                    try {
                                        mView.uploadSuccess(type, data.getId(), iv, uri);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }));
                }));


    }

    @Override
    public void postRealname(String name, String idCard, String cityCode, String id) {
        addSubscribe(httpManager.postRealName(name, idCard, cityCode, id)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.onPostSuccess();
                    }
                }));
    }

    @Override
    public void postRealname(CertRequest request) {
        addSubscribe(httpManager.cert(request)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.onPostSuccess();
                    }
                }));
    }

    @Override
    public void getRealName() {
        addSubscribe(httpManager.getRealName().compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonBean>(mView) {
                    @Override
                    public void onSuccess(CommonBean data) {
                        try {
                            mView.onGetRealName(data);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }


}
